package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.ReissueCardMapper;
import com.rs.model.CommonResult;
import com.rs.model.User;
import com.rs.model.Attendance;
import com.rs.model.ReissueCard;
import com.rs.service.rs.ReissueCardService;
import com.rs.util.FileOperationsUtil;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class ReissueCardServiceImpl implements ReissueCardService {
	@Autowired
	private ReissueCardMapper rm;

	/***********APP************/

	/**
	   * 添加补卡记录
	 */
	@Override
	@Transactional
	public CommonResult<String> reissueCardAddApp(ReissueCard rCard) {
		//判断是否有图片
		boolean flag = Optional.ofNullable(rCard.getRes_base64()).isPresent();
		if (flag) {//有图片
			String base64 = rCard.getRes_base64();//前端传的图片
			String rootPath = "C:/CompayRs/reissueCard";//上传的文件夹	
			String fileType = rCard.getRes_imgType();//文件后缀
			String path = FileOperationsUtil.getFilePath(base64, rootPath, fileType);
			rCard.setRes_img(path);//将图片地址赋值给对象
		}
		//没有图片不用赋值
		rCard.setRes_audit(0);//将审核状态设为待审核
		rCard.setRes_retime(new Date());//设置补卡时间为当前时间
		int count = rm.reissueCardAddApp(rCard);
		if (count != 0) {//补卡成功
			return new CommonResult<String>(200, "补卡成功");
		} else {

			return new CommonResult<String>(200, "补卡失败");
		}
	}

	/**
	 * 查看用户的所有补卡申请记录
	 */
	@Override
	public CommonResult<List<ReissueCard>> reissueCardUserLookApp(ReissueCard reissueCard) {
		List<ReissueCard> list = rm.reissueCardUserLookApp(reissueCard);
		return new CommonResult<List<ReissueCard>>(200, "查询成功", list);
	}

	/**
	 * 查看所有待审核的补卡记录
	 */
	@Override
	public CommonResult<List<ReissueCard>> reissueCardLookAllToAuditApp() {
		List<ReissueCard> list = rm.reissueCardLookAllToAuditApp();
		return new CommonResult<List<ReissueCard>>(200, "查询成功", list);
	}

	/**
	 * 查询补卡申请的详情
	 */
	@Override
	public CommonResult<ReissueCard> reissueCardLookOneApp(Integer res_id) {
		ReissueCard re = rm.reissueCardLookOneApp(res_id);
		return new CommonResult<ReissueCard>(200, "查询成功", re);
	}

	/**
	 * 审核补卡记录
	 */
	@Override
	@Transactional
	public CommonResult<String> reissueCardAuditApp(ReissueCard reissueCard) {
		// 审核补卡记录
		int count = rm.reissueCardAuditApp(reissueCard);
		if (count != 0) {//审核成功
			int state = reissueCard.getRes_audit();//审核的状态

			if (state == 1) {//审核通过，修改考勤记录

				//审核通过时,获取此条补卡记录的信息
				ReissueCard ress = rm.getAuditReissueCard(reissueCard.getRes_id());

				int res_atType = ress.getRes_atType();//考勤时间选择(1:早 2：上午 3：下午 4：晚)")

				Attendance attendance = new Attendance();//考勤对象
				//考勤类别（0：未打卡 1：打卡 2：迟到 3：早退 4：缺卡 5：补卡 6：请假 7：出差）
				attendance.setAt_id(ress.getRes_atid());//设置考勤记录的id

				switch (res_atType) {
				case 1://早
					attendance.setAt_amtype(5);//设置早上的为补卡
					break;
				case 2://上午
					attendance.setAt_noontype(5);//设置上午的为补卡
					break;
				case 3://下午
					attendance.setAt_pmtype(5);//设置下午的为补卡
					break;
				case 4://晚上
					attendance.setat_nighttype(5);//设置晚上的为补卡
					break;
				}
				int co = rm.rCardAuditUpdateAttendance(attendance);//审核通过时修改考勤记录表
				if (co == 0) {//考勤表修改失败
					//手动回滚事务
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return new CommonResult<String>(200, "审核失败");
				}

			}
			return new CommonResult<String>(200, "审核成功");

		} else {

			return new CommonResult<String>(200, "审核失败");
		}
	}

	/**
	 * 删除补卡记录
	 */
	@Override
	@Transactional
	public CommonResult<String> reissueCardDeleteApp(Integer res_id) {
		int count = rm.reissueCardDeleteApp(res_id);
		if (count != 0) {
			return new CommonResult<String>(200, "删除成功");
		} else {
			return new CommonResult<String>(200, "删除失败");
		}

	}

	/**
	 * pc端的条件查询
	 * @throws JsonProcessingException 
	 */
	@Override
	public CommonResult<Map<String, Object>> reissueCardConditionsQueryPc(ReissueCard reissueCard) {

		Page<User> page = PageHelper.startPage(reissueCard.getPageNum(), 10);//分页
		List<ReissueCard> list = rm.reissueCardConditionsQueryPc(reissueCard);

		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());//总条数
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc端的补卡时间查询
	 */
	@Override
	public CommonResult<Map<String, Object>> reissueCardTimeQueryPc(Map<String, String> map) {
		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		Page<User> page = PageHelper.startPage(pageNum, 10);//分页
		List<User> list = rm.userEntryTimeQueryPc(startTime, endTime);//list
		//map
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

}
